
import {lift} from "./util.js";
import {defineTabButtons} from "./buttons.js";
import {defineFocusNav} from "./focusNav.js";
import {defineRovingTabindexByHandle} from "./rovingTabindex.js";

/**
 * Implements WAI-ARIA Radio Group pattern: https://www.w3.org/TR/wai-aria-practices/#tabpanel
 *
 * @param tablist d3 selection of one or more tablist containers
 * @param {Handle} handle the field in the backing store that stores the name of the selected tab
 * @param {Function?} fn optional callback ƒ(name, meta) to perform the store update, passing in the name of clicked
 *        tab and the meta object to use when mutating the store. When not specified, the default behavior is to
 *        store the name in the handle directly.
 */
export function defineTablist(tablist, handle, fn) {
    tablist.each(lift(list => {
        const tabs = list.selectAll("[role=tab]");
        defineTabButtons(tabs, handle, fn);
        defineFocusNav(tabs, {enableUpDown: false});
        defineRovingTabindexByHandle(tabs, handle);
    }));
}
